package com.ehotting.edsta.circuitDesign.gen.element.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.domain.EsSceneParam;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import com.ehotting.edsta.circuitDesign.gen.element.helper.ElementScriptHelper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RangeElementScript extends ElementScriptAbstract {

    /***
     * -------------范围------------
     * 范围的值计算，主要是为某个元器件，找出符合某个值或者某个范围的值
     * @param esRuleScript
     * @param inputParamList
     * @param outputParamList
     * @return
     */
    public String invoke(EsRuleScript esRuleScript, List<EsCircuitCodeDataTemplate> inputParamList, List<EsSceneParam> outputParamList, String functionName){
        outputParamList = findXElementList(esRuleScript.getContent(), outputParamList);
        // ------------------------------------脚本组装------------------------------------

        StringBuffer buf = new StringBuffer();

        /**1、定义方法头部 **/
        buf.append(functionStart( functionName, inputParamList));

        /**2、定义变量 **/
        buf.append(defineVariables( outputParamList));

        /**3、业务逻辑实现**/
        buf.append(genBody(esRuleScript, outputParamList));

        /**4、处理结果值**/
        buf.append(handleResult(outputParamList));

        /**5、返回状态**/
        buf.append(ScriptConstants.TAB + "return ElementReturnHelper.success();"+ ScriptConstants.LINE);

        /**6、判断结尾**/
        buf.append( "};"+ ScriptConstants.LINE);

        return  buf.toString();
    }



    public String defineVariables(List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        for( EsSceneParam esSceneParam : outputParamList) {
            buf.append(ScriptConstants.TAB + "List<EsElement> "+esSceneParam.getCode()+"ComponentList = new ArrayList();" + ScriptConstants.LINE);
        }
        return buf.toString();
    }

    /**
     * 业务逻辑实现
     * @param esRuleScript
     * @param outputParamList
     * @return
     */
    public String genBody(EsRuleScript esRuleScript, List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        String[] lines = esRuleScript.getContent().split(ScriptConstants.LINE);
        for(int index=0;index<lines.length;index++) {
            buf.append(genBody(lines[index], outputParamList));
        }
        return buf.toString();
    }


    /**
     * 逻辑主体
     * @param runScript
     * @param outputParamList
     * @return
     */
    public String genBody(String runScript,List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        // 1、定义for循环
        buf.append(this.startFor(runScript,outputParamList));
        int i = outputParamList.size();
        // 2、定义条件
        buf.append(ScriptConstants.TAB(i+1)).append("if("+ ElementScriptHelper.replaceNodeToUnderline(runScript)+"){"+ ScriptConstants.LINE) ;
        // 3、定义赋值
        buf.append(setValue(outputParamList, i));
        // 4、判断结尾
        buf.append(ScriptConstants.TAB(i+1)).append( "}"+ ScriptConstants.LINE);
        // 5、定义for循环的结尾
        buf.append(this.endFor(i));

        return buf.toString();
    }

    /**
     * 设置结果值
     * @param outputParamList
     * @param i
     * @return
     */
    public String setValue(List<EsSceneParam> outputParamList,int i){
        StringBuffer buf = new StringBuffer();
        buf.append(ScriptConstants.TAB(i+2));
        buf.append( "java.util.Map<String,Object> map = new java.util.HashMap();"+ ScriptConstants.LINE);

        for (EsSceneParam esSceneParam : outputParamList){
            buf.append(ScriptConstants.TAB(i+2));
            buf.append( "map.put(\""+esSceneParam.getCode()+"\","+esSceneParam.getCode()+"Obj);"+ ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(i+2));
            buf.append( "ElementDataHelper.addIfNotExist("+esSceneParam.getCode()+"ComponentList,"+esSceneParam.getCode()+"Obj);"+ ScriptConstants.LINE);
        }
        buf.append(ScriptConstants.TAB(i+2));
        buf.append( "outMapList.add(map);"+ ScriptConstants.LINE);

        return buf.toString();
    }

    /**
     * 处理结果值
     * @param outputParamList
     * @return
     */
    public String handleResult(List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        for( EsSceneParam esSceneParam : outputParamList) {
            buf.append(ScriptConstants.TAB + esSceneParam.getCode()+"List.clear();" + ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB + esSceneParam.getCode()+"List.addAll("+esSceneParam.getCode()+"ComponentList);" + ScriptConstants.LINE);
        }
        return buf.toString();
    }


    public boolean isTrue(ScriptTypeEnum code){
        return ScriptTypeEnum.RANGE ==  code ;
    }

    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式参数:");
        buf.append("输入格式:");
        buf.append("r1/r2 > 200").append(ScriptConstants.LINE);
        buf.append("或者多个条件，可以使用逻辑符号 \"&&\"、\"||\"分割:");
        buf.append("r1 >= 200 && r2 >= 200").append(ScriptConstants.LINE);
        buf.append("或者属性:");
        buf.append("r1.xx >= 200").append(ScriptConstants.LINE);
        return buf.toString();
    }
}
